﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TheSnakeGame.Engine;
using TheSnakeGame.Engine.GameObjects;
using TheSnakeGame.WpfClient.Views;
using TheSnakeGame.WpfClient.DrawableObjects;
using System.Windows.Threading;

namespace TheSnakeGame.WpfClient.ViewModels
{
    public class GameEngineViewModel : BaseViewModel
    {
        public string Title 
        {
            get { return "The Snake Game Test level"; }
        }

        public IView View 
        { 
            get;
            set; 
        }

        public ObservableCollection<DrawableObject> Walls 
        { 
            get;
            set;
        }

        public ObservableCollection<DrawableObject> SnakeBody 
        { 
            get; 
            set; 
        }

        public DrawableObject SnakeHead
        {
            get;
            set;
        }

        public GameEngineViewModel()
        {
            var gameEngine = new GameEngine(50, 50);
            gameEngine.PerfostmStandartInitialization();

            var walls = gameEngine.BarierWalls;
            this.Walls = new ObservableCollection<DrawableObject>(walls.Select(x=>new DrawableObject(x)));

            var snakeParts = gameEngine.Snake.Body;
            this.SnakeBody = new ObservableCollection<DrawableObject>(snakeParts.Select(x => new DrawableObject(x)));

            this.SnakeHead = new DrawableObject(gameEngine.Snake);

            var dt = new DispatcherTimer();
            dt.Interval = TimeSpan.FromMilliseconds(500);
            dt.Tick += (sender, e) =>
                {
                    gameEngine.ProcessGameTick();
                    this.OnPropertyChanged("SnakeHead");
                    this.OnPropertyChanged("SnakeBody");
                    gameEngine.Snake.MoveDirection = Directions.Bottom;
                };
            dt.Start();
        }

    }
}
